/**
 * InputText.as
 * Keith Peters
 * version 0.9.10
 * 
 * Copyright (c) 2011 Keith Peters
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
 
package com.wskeee.ec.components
{
	import com.wskeee.ec.core.EComponent;
	import com.wskeee.ec.styles.DefaultStyle;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	public class TextInput extends EComponent
	{
		/**
		 * 构造函数 
		 * @param text		默认显示文字
		 * 
		 */
		public function TextInput(text:String = "")
		{
			this.text = text;
			super();
			border = true;
			setActualSize(100,22);
		}
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		protected var _back:Sprite;
		protected var _password:Boolean = false;
		protected var _text:String = "";
		protected var _tf:TextField;
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		//-----------------------------------------------------------------
		//  get & set
		//-----------------------------------------------------------------
		/**
		 * 设置显示内容
		 */
		public function set text(t:String):void
		{
			_text = t;
			if(_text == null) _text = "";
			invalidateProperties();
			invalidateSize();
		}
		public function get text():String
		{
			return _text;
		}
		
		/**
		 * textField，可引用直接设置  textField 属性.
		 */
		public function get textField():TextField
		{
			return _tf;
		}
		
		/**
		 * 输入限制，eg:0-9(只允许输入数据),a-z|A-Z(允许小写与大写英文输入)
		 */
		public function set restrict(str:String):void
		{
			_tf.restrict = str;
		}
		public function get restrict():String
		{
			return _tf.restrict;
		}
		
		/**
		 * 输入最大字符数
		 */
		public function set maxChars(max:int):void
		{
			_tf.maxChars = max;
		}
		public function get maxChars():int
		{
			return _tf.maxChars;
		}
		
		/**
		 * 是否是以密码(*)显示内容
		 */
		public function set password(b:Boolean):void
		{
			_password = b;
			_tf.displayAsPassword = b;
		}
		public function get password():Boolean
		{
			return _password;
		}
		
		/**
		 * 设置段落对齐方式 
		 * @param value		left/center/right
		 * 
		 */
		public function set align(value:String):void
		{
			if(value!="left" && value!="center" && value!="right")
				value="left";
			setStyle("align",value);
		}
		public function get align():String
		{
			return getStyle("align") as String;
		}
		
		//---------------------
		//edit
		//---------------------
		private var _edit:Boolean = true;
		/**
		 * 启用/禁用 TextInput 编辑功能
		 * @param bo
		 * 
		 */
		public function set edit(bo:Boolean):void
		{
			_edit = bo;
			invalidateProperties();
		}
		public function get edit():Boolean
		{
			return _edit;
		}
		
		//---------------------
		//autoSize
		//---------------------
		private var _autoSize:Boolean = false;
		public function set autoSize(bo:Boolean):void
		{
			_autoSize = bo;
			invalidateSize();
		}
		public function get autoSize():Boolean
		{
			return _autoSize;
		}
		
		/**
		 * 启用/禁用
		 */
		public override function set enabled(value:Boolean):void
		{
			super.enabled = value;
			_tf.tabEnabled = value;
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Method
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  init
		//-----------------------------------------------------------------
		//---------------------
		//initChildren
		//---------------------
		override protected function initChildren():void
		{
			super.initChildren();
			_back = new Sprite();
			addChild(_back);
			
			_tf = new TextField();
			_tf.selectable = true;
			_tf.type = TextFieldType.INPUT;
			addChild(_tf);
			_tf.addEventListener(Event.CHANGE, onChange);
			
			reflashStyle();
		}
		
		//-----------------------------------------------------------------
		//  redraw
		//-----------------------------------------------------------------
		override public function reflashStyle():void
		{
			super.reflashStyle();
			var g:Graphics = _back.graphics;
			g.clear();
			g.beginFill(getStyle("borderColor") as uint,1);
			g.drawRect(0,0,10,10);
			g.beginFill(getStyle("backgroundColor") as uint,1);
			g.drawRect(1,1,8,8);
			_back.scale9Grid = new Rectangle(1,1,8,8);
			
			_back.filters = [DefaultStyle.getShadow(2,true)];
		}
		
		//--------------------------------------------------------------------------
		//
		//  validate Method
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  commitProperties
		//-----------------------------------------------------------------
		override protected function commitProperties():void
		{
			super.commitProperties();
			if(_tf.text!=_text)
			{
				var textFormat:TextFormat = _tf.getTextFormat();
				textFormat.align = getStyle("align") as String;
				textFormat.font = getStyle("fontFamily") as String;
				textFormat.size = getStyle("fontSize");
				textFormat.color = getStyle("fontColor");
				_tf.defaultTextFormat = textFormat;
				_tf.text = _text==null ? "" : _text;
				_tf.type = edit ? TextFieldType.INPUT : TextFieldType.DYNAMIC;
			}
		}
		override protected function measure():void
		{
			super.measure();
			var _mWidth:Number = width,_mHeight:Number = height;
			if(isNaN(explicitHeight))
			{
				if(_text == "")
				{
					_tf.text = "X";
					_mHeight = Math.max(_tf.textHeight+2,_mHeight);
					_tf.text = "";
				}else
				{
					_mHeight = Math.max(_tf.textHeight+2,_mHeight);
				}
			}
			
			if(_autoSize)
			{
				_mWidth = _tf.textWidth + 8;
				_mWidth = Math.max(_mWidth,10);
				_tf.scrollH = 0;
			}
			setActualSize(_mWidth,_mHeight);
		}
		override protected function updateDisplayList():void
		{
			super.updateDisplayList();
			_back.width = width;
			_back.height = height;
			_tf.width = width - 4;
			_tf.height = height - 4 ;
			_tf.x = 2;
			_tf.y = height / 2 - _tf.height / 2;
		}
		
		//--------------------------------------------------------------------------
		//
		//  event handlers
		//
		//--------------------------------------------------------------------------
		
		/**
		 * Internal change handler.
		 * @param event The Event passed by the system.
		 */
		protected function onChange(event:Event):void
		{
			_text = _tf.text;
			event.stopImmediatePropagation();
			dispatchEvent(event);
			invalidateProperties();
			invalidateSize();
		}
	}
}